/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ContentManagers.Security;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Ervin James
 */
public class Hasher {
    
       /*
        * returns a hashed String representation of a string
        */
      public static String getHash(int iterationNb, String password, byte[] salt) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-512");
        digest.reset();
        digest.update(salt);
        byte[] input=null;
        String hash;
        try {
            
            input = digest.digest(password.getBytes("UTF-8"));
            
            } catch (UnsupportedEncodingException ex) {
            
                Logger.getLogger(Hasher.class.getName()).log(Level.SEVERE, null, ex);
             }
        
       for (int i = 0; i < iterationNb; i++) {
           digest.reset();
           input = digest.digest(input);
       }
       
       hash = byteToBase64(input);
       
       return hash;
   }
         
        /*
        * returns a byte representation of a random salt
        */ 
   public static byte[] generateRandomSalt(){
        SecureRandom random =null;
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Hasher.class.getName()).log(Level.SEVERE, null, ex);
        }
        // Salt generation 64 bits long
        byte[] bSalt = new byte[8];
       
        random.nextBytes(bSalt);
        
        return bSalt;
   }   
   /*
    * From a base 64 representation, returns the corresponding byte[] 
    */
   public static byte[] base64ToByte(String data) throws IOException {
       BASE64Decoder decoder = new BASE64Decoder();
       return decoder.decodeBuffer(data);
   }
   /*
    * From a byte[] returns a base 64 representation
    */
   public static String byteToBase64(byte[] data){
       BASE64Encoder endecoder = new BASE64Encoder();
       return endecoder.encode(data);
   }
   
   public static String[] processPassword(String password1)
    {   String hashedPass[] = new String[2];
        hashedPass[0] = "null";
        hashedPass[1] = "null";
        String sDigest;
        String sSalt;
        byte[] bSalt;
    
        try{
            //process hashing here
          
            bSalt = Hasher.generateRandomSalt();
            sDigest = Hasher.getHash(1, password1, bSalt);
            sSalt = Hasher.byteToBase64(bSalt);
            
            hashedPass[0] = sDigest;
            hashedPass[1] = sSalt;
            
            }   
            catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Hasher.class.getName()).log(Level.SEVERE, null, ex);
            }

        //newCustomer.setpas
        return hashedPass;
    }
   public static String[] generateHashedSession(String usernameIP)
    {   String hashedPass[] = new String[2];
        hashedPass[0] = "null";
        hashedPass[1] = "null";
        String sDigest;
        String sSalt;
        byte[] bSalt;
    
        try{
            //process hashing here
          
            bSalt = Hasher.generateRandomSalt();
            sDigest = Hasher.getHash(1, usernameIP, bSalt);
            sSalt = Hasher.byteToBase64(bSalt);
            
            hashedPass[0] = sDigest;
            hashedPass[1] = sSalt;
            
            }   
            catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Hasher.class.getName()).log(Level.SEVERE, null, ex);
            }
        
    
        //newCustomer.setpas
        return hashedPass;
    }
   
         
}
